package com.demo.service;

import com.demo.dto.Studentdto;

import java.util.ArrayList;
import java.util.List;

public class Studentservice {

    private List<Studentdto> list;

    public List<Studentdto> getAllStudent() {
        if (this.list == null) {
            this.list = new ArrayList<>();
        } else {
            for (int i = 0; i < this.list.size(); i++) {
                Studentdto studentdto = this.list.get(i);
                System.out.println("学生 ID" + studentdto.getId() + "名字" + studentdto.getName() + "学生年龄" + studentdto.getAge());
            }
        }
        System.out.println("一共有多少名学生" + this.list.size());
        return list;
    }

    public boolean addStudent(Studentdto studentdto) {
        this.list.add(studentdto);
        System.out.println("添加了一个学生===" + studentdto.getName());
        return false;
    }

    public Studentdto query(int id) {
        Studentdto studentdto2 = null;
        try {
            for (int i = 0; i < this.list.size(); i++) {
                studentdto2 = list.get(i);
                if (id == studentdto2.getId()) {
                    System.out.println(studentdto2);

                   // return studentdto2;
                    break;
                }
            }
            System.out.println("没有找到id为"+id+"的学生");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }


    public boolean updateStudent(int id, Studentdto studentdto) {
        boolean flag = false;
        try {
            int count = 0;
            for (int i = 0; i < this.list.size(); i++) {
                Studentdto studentdto1 = list.get(i);
                if (id == studentdto1.getId()) {
                    studentdto1.setAge(studentdto.getAge());
                    studentdto1.setName(studentdto.getName());
                } else {
                    count++;
                }
            }

            if (count == list.size()) {
                System.out.println("id=" + id + "为找到修改的人!");
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return flag;
    }


    public boolean removeStudent(int studentId) {
        //检查列表是否为空
        if (this.list == null) {
            System.out.println("学生列表为空，无法删除学生。");
            return false;
        }
        //检查学生表，找到指定ID的学生
        for (int i = 0; i < this.list.size(); i++) {
            //添加变量studentdto，
            Studentdto studentdto = this.list.get(i);
            if (studentdto.getId() == studentId) {
                this.list.remove(i);
                System.out.println("已删除学生: ID=" + studentdto.getId() + ", 名字=" + studentdto.getName());
                return true;
            }
        }
        System.out.println("未找到指定ID的学生，无法删除。");
        return false;


    }


    public boolean removeStudent() {
        this.list.remove(3);
        return true;
    }

}
